<?php

// module/Site/src/Album/Controller/AlbumController.php:

namespace Site\Controller;

use Site\Model\Users;
use Site\Form\UsersForm;
use Zend\Mvc\Controller\AbstractActionController;
use Zend\View\Model\ViewModel;

class SiteController extends AbstractActionController {

    protected $usersTable;

    public function indexAction() {
        return new ViewModel(array(
                    'users' => $this->getUserTable()->fetchAll(),
                ));
    }

    public function addAction() {
        $form = new UsersForm();
        $form->get('submit')->setValue('Add');

        $request = $this->getRequest();
        if ($request->isPost()) {
            $users = new Users();
            $form->setInputFilter($users->getInputFilter());
            $form->setData($request->getPost());

            if ($form->isValid()) {
                $users->exchangeArray($form->getData());
                $this->getUserTable()->saveUsers($users);

                // Redirect to list of albums
                return $this->redirect()->toRoute('site');
            }
        }
        return array('form' => $form);
    }

    public function editAction() {


        $id = (int) $this->params()->fromRoute('id', 0);
        if (!$id) {
            return $this->redirect()->toRoute('site', array(
                        'action' => 'index'
                    ));
        }

        // Get the Album with the specified id.  An exception is thrown
        // if it cannot be found, in which case go to the index page.
        try {
            $album = $this->getUserTable()->getUsers($id);
        } catch (\Exception $ex) {
            return $this->redirect()->toRoute('site', array(
                        'action' => 'index'
                    ));
        }

        $form = new UsersForm();
        $form->bind($album);
        $form->get('submit')->setAttribute('value', 'Edit');

        $request = $this->getRequest();
        if ($request->isPost()) {
            $form->setInputFilter($album->getInputFilter());
            $form->setData($request->getPost());

            if ($form->isValid()) {
                $this->getUserTable()->saveUsers($form->getData());

                // Redirect to list of albums
                 return $this->redirect()->toRoute('site', array(
                            'action' => 'index'
                        ));
            }

        }

        return array(
            'id' => $id,
            'form' => $form,
        );
    }

    public function getUserTable() {
        if (!$this->usersTable) {
            $sm = $this->getServiceLocator();
            $this->usersTable = $sm->get('Site\Model\UsersTable');
        }
        return $this->usersTable;
    }

    public function deleteAction() {
        $id = (int) $this->params()->fromRoute('id', 0);
        //    return array(
        //     'id' => $id,
        // );

        $id = (int) $this->params()->fromRoute('id', 0);
        if (!$id) {             
            return $this->redirect()->toRoute('site', array(
                            'action' => 'index'
                        ));
        }

        $request = $this->getRequest();
        if ($request->isPost()) {
            $del = $request->getPost('del', 'No');

            if ($del == 'Yes') {
                $id = (int) $request->getPost('id');
                $this->getUserTable()->deleteUser($id);
            }

            // Redirect to list of albums
            return $this->redirect()->toRoute('site');
        }

        return array(
            'id' => $id,
            'user' => $this->getUserTable()->getUsers($id)
        );
    }

}
